package org.qiyu.live.bank.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.idea.qiyu.live.framework.redis.starter.key.BankProviderCacheKeyBuilder;
import org.qiyu.live.bank.dto.PayProductDTO;
import org.qiyu.live.bank.provider.dao.mapper.IPayProductMapper;
import org.qiyu.live.bank.provider.dao.po.PayProductPO;
import org.qiyu.live.bank.provider.service.IPayProductService;
import org.qiyu.live.common.interfaces.enums.CommonStatusEnum;
import org.qiyu.live.common.interfaces.utils.ConvertBeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-11-04
 * Time: 22:23
 */
@Service
public class PayProductServiceImpl implements IPayProductService {

    @Resource
    private IPayProductMapper productMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private BankProviderCacheKeyBuilder cacheKeyBuilder;

    @Override
    public List<PayProductDTO> products(Integer type) {
        String typeKey = cacheKeyBuilder.buildProductListCacheKey(type);
        // 1. 先去redis中查询有没有list
        List<PayProductDTO> payProductDTOS = redisTemplate.opsForList().range(typeKey, 0, 30).stream().map(x -> ConvertBeanUtils.convert(x, PayProductDTO.class)).toList();
        // 2. 如果有直接返回list
        if (!CollectionUtils.isEmpty(payProductDTOS)) {
            if (payProductDTOS.get(0).getId() == null) {
                return Collections.emptyList();
            }
            return payProductDTOS;
        }
        // 3. 如果没有就去查询数据库
        LambdaQueryWrapper<PayProductPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayProductPO::getType, type);
        queryWrapper.eq(PayProductPO::getValidStatus, CommonStatusEnum.VALID.getCode());
        payProductDTOS = ConvertBeanUtils.convertList(productMapper.selectList(queryWrapper), PayProductDTO.class);
        if (CollectionUtils.isEmpty(payProductDTOS)) {
            // 4. 数据库不存在我们就缓存一个空值
            redisTemplate.opsForList().leftPush(typeKey, new PayProductDTO());
            redisTemplate.expire(typeKey, 3, TimeUnit.MINUTES);
            return Collections.emptyList();
        }
        // 5. 如果存在,缓存一下,就直接返回
        redisTemplate.opsForList().leftPushAll(typeKey, payProductDTOS.toArray());
        redisTemplate.expire(typeKey, 30, TimeUnit.MINUTES);
        return payProductDTOS;
    }

    @Override
    public PayProductDTO getPayProductById(Integer productId) {
        String cacheKey = cacheKeyBuilder.buildProductCacheKey(productId);
        PayProductDTO payProductDTO = (PayProductDTO) redisTemplate.opsForValue().get(cacheKey);
        if (payProductDTO != null) {
            if (payProductDTO.getId() == null) {
                return null;
            }
            return payProductDTO;
        }
        PayProductDTO productDTO = ConvertBeanUtils.convert(productMapper.selectById(productId), PayProductDTO.class);
        if (productDTO != null) {
            redisTemplate.opsForValue().set(cacheKey, productDTO,30,TimeUnit.MINUTES);
            return productDTO;
        }
        redisTemplate.opsForValue().set(cacheKey, new PayProductDTO(),5,TimeUnit.MINUTES);
        return null;
    }
}